package com.maila.xiaoaoschool.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.maila.xiaoaoschool.R;
import com.pl.wheelview.WheelView;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 2017/12/18.
 */

public class SetSexPop extends PopupWindow {
    private View mMenuView;
    private TextView btncancel;
    private TextView btnconfirm;
    private WheelView wheelView;
    ArrayList<String> sexes=new ArrayList<>();
    String sex="男";
    SetSexLinstner sexLinstner;
    public SetSexPop(final Context context,SetSexLinstner setSexLinstner) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_set_sex, null);
        AutoUtils.autoSize(mMenuView);
        btncancel = (TextView) mMenuView.findViewById(R.id.tv_cancel);
        btnconfirm = (TextView) mMenuView.findViewById(R.id.tv_confirm);
        wheelView=mMenuView.findViewById(R.id.wv_sex);
        sexLinstner=setSexLinstner;
        sexes.add("男");
        sexes.add("女");
        wheelView.setCyclic(false);
        wheelView.setDefault(0);
        wheelView.setData(sexes);
        wheelView.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                sex=text;

            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        btncancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
            }
        });
        btnconfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sexLinstner.selectsex(sex);
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
//            设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//            设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);

        ColorDrawable dw = new ColorDrawable(00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.popu_comment).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    }
public interface SetSexLinstner{
        void selectsex(String sex);
}
}
